//
//  WmcCustomViewProtocol.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/20.
//

import SnapKit
import UIKit
import YPBedrock

// MARK: - [WmcCustomViewProtocol]

/// 自定义视图协议
public protocol WmcCustomViewProtocol where Self: UIView {
    /// 设置页面推出时的样式
    /// 支持上下左右和透明度渐变
    ///   - attribute: 当前水印相机的属性
    /// - Returns: 推出时的样式
    func pushTransitionStyle(_ attribute: WmcAttribute) -> WmcTransition.Style

    /// 设置页面退出时的样式
    /// 支持上下左右和透明度渐变
    ///   - attribute: 当前水印相机的属性
    /// - Returns: 推出时的样式
    func popTransitionStyle(_ attribute: WmcAttribute) -> WmcTransition.Style

    /// 设置页面推出时的动画
    /// 支持上下左右和透明度渐变
    ///   - attribute: 当前水印相机的属性
    /// - Returns: 推出时的动画
    func pushTransitionAnimated(_ attribute: WmcAttribute) -> Bool

    /// 设置页面退出时的动画
    /// 支持上下左右和透明度渐变
    ///   - attribute: 当前水印相机的属性
    /// - Returns: 推出时的动画
    func popTransitionAnimated(_ attribute: WmcAttribute) -> Bool

    /// 视图即将加载
    /// - Parameter attribute: 当前水印相机属性
    func viewDidLoad(_ attribute: WmcAttribute)

    /// 某个页面准备出现
    /// - Parameters:
    ///   - attribute: 当前水印相机的属性
    ///   - animated: 动画执行
    func viewWillAppear(_ attribute: WmcAttribute,
                        animated: Bool)

    /// 某个页面完全出现
    /// - Parameters:
    ///   - attribute: 当前水印相机的属性
    ///   - animated: 动画执行
    func viewDidAppear(_ attribute: WmcAttribute,
                       animated: Bool)

    /// 某个页面即将消失
    /// - Parameters:
    ///   - attribute: 当前水印相机的属性
    ///   - animated: 动画执行
    func viewWillDisappear(_ attribute: WmcAttribute,
                           animated: Bool)

    /// 某个页面已经消失
    /// - Parameters:
    ///   - attribute: 当前水印相机的属性
    ///   - animated: 动画执行
    func viewDidDisappear(_ attribute: WmcAttribute,
                          animated: Bool)

    /// 点击返回时触发
    /// - Parameter attribute: 当前水印相机属性
    /// - Returns: true or false 决定是否返回 true允许返回
    func backAction(_ attribute: WmcAttribute) -> Bool

    /// 是否使用导航
    /// - Parameter attribute: 当前水印相机属性
    /// - Returns: true 使用 false 不使用
    func navigationBarUse(_ attribute: WmcAttribute) -> Bool

    /// 设置导航条样式
    /// - Parameter attribute: 当前水印相机属性
    /// - Returns: 导航样式，白色、深色、透明
    func navigationBarStyle(_ attribute: WmcAttribute) -> WmcBaseVC.NavStyle

    /// 是否需要导航底部阴影
    /// - Parameter attribute: 当前水印相机属性
    /// - Returns: true or false
    func navigationBarUseShadow(_ attribute: WmcAttribute) -> Bool

    /// 在这里进行布局UI
    /// - Parameter attribute: 当前水印相机属性
    func makeUI(_ attribute: WmcAttribute)

    /// 在这里进行绑定视图事件
    /// - Parameter attribute: 当前水印相机属性
    func bindViewAction(_ attribute: WmcAttribute)

    /// 在这里进行绑定VM事件
    /// - Parameter attribute: 当前水印相机属性
    func bindViewModel(_ attribute: WmcAttribute)
}

// MARK: - 驱动方法

public extension WmcCustomViewProtocol {
    /// 设置标题
    /// - Parameter title: 标题
    func wmc_setTitle(_ title: String) {
        YPWmcDrivingManager.shared.custom_setTitleBlock?(title)
    }

    /// 返回VC
    func wmc_popViewController() {
        YPWmcDrivingManager.shared.custom_backViewControllerBlock?()
    }

    /// 推出VC
    /// - Parameters:
    ///   - vc: VC
    ///   - style: 推出样式
    ///   - animated: 动画
    /// - Returns: 控制器
    @discardableResult
    func wmc_pushCustomView(_ view: WmcCustomViewProtocol)
        -> UIViewController?
    {
        return YPWmcDrivingManager.shared.custom_pushCustomViewBlock?(view)
    }

    /// 使用路由推出内置VC
    /// - Parameters:
    ///   - navigationKey: 路由键值
    ///   - style: 推出样式
    ///   - viewModel: 视图模型
    ///   - animated: 动画
    /// - Returns: 控制器
    @discardableResult
    func wmc_pushNavigation(_ navigationKey: WmcNavigatorManager.Path,
                            style: WmcTransition.Style? = nil,
                            viewModel: WmcBaseVM? = nil,
                            animated: Bool = true,
                            completion: YPBlock.Empty? = nil)
        -> UIViewController?
    {
        return YPWmcDrivingManager.shared.custom_pushNavigationBlock?(navigationKey, style, viewModel, animated, completion)
    }

    /// 拉起一个sheet弹窗
    /// - Parameters:
    ///   - view: 弹窗视图
    ///   - layoutBlock: 弹窗相对Window的布局
    ///   - isTouchMaskDismiss: 点击遮罩时，是否自动返回
    ///   - maskBackgroundColor: 遮罩背景色
    ///   - hollowRects: 需要镂空的地方
    ///   - touchMaskBlock: 点击遮罩时的回调
    /// - Returns: 返回模态控制器
    @discardableResult
    func wmc_presentSheet(_ view: UIView,
                          layoutBlock: @escaping YPBlock.OneParam<ConstraintMaker>,
                          isTouchMaskDismiss: Bool = true,
                          maskBackgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                          hollowRects: [WmcPresentVC.Config.HollowRect] = [],
                          touchMaskBlock: @escaping YPBlock.Empty = {})
        -> WmcPresentVC
    {
        let config = createPresentConfig(style: .sheet(),
                                         view: view,
                                         layoutBlock: layoutBlock,
                                         isTouchMaskDismiss: isTouchMaskDismiss,
                                         backgroundColor: maskBackgroundColor,
                                         hollowRects: hollowRects,
                                         touchMaskBlock: touchMaskBlock)
        let presentVC = WmcPresentVC(config: config)
        presentVC.show()
        return presentVC
    }

    /// 拉起一个alert弹窗
    /// - Parameters:
    ///   - view: 弹窗视图
    ///   - layoutBlock: 弹窗相对Window的布局
    ///   - isTouchMaskDismiss: 点击遮罩时，是否自动返回
    ///   - maskBackgroundColor: 遮罩背景色
    ///   - hollowRects: 需要镂空的地方
    ///   - touchMaskBlock: 点击遮罩时的回调
    /// - Returns: 返回模态控制器
    @discardableResult
    func wmc_presentAlert(_ view: UIView,
                          layoutBlock: @escaping YPBlock.OneParam<ConstraintMaker>,
                          isTouchMaskDismiss: Bool = true,
                          maskBackgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                          hollowRects: [WmcPresentVC.Config.HollowRect] = [],
                          touchMaskBlock: @escaping YPBlock.Empty = {})
        -> WmcPresentVC
    {
        var config = createPresentConfig(style: .alert(),
                                         view: view,
                                         layoutBlock: layoutBlock,
                                         isTouchMaskDismiss: isTouchMaskDismiss,
                                         backgroundColor: maskBackgroundColor,
                                         hollowRects: hollowRects,
                                         touchMaskBlock: touchMaskBlock)
        config.isTouchMaskDismiss = false
        let presentVC = WmcPresentVC(config: config)
        presentVC.show()
        return presentVC
    }
}

// MARK: - =========================⚠️以下对接业务方无需查看或了解⚠️=========================

// MARK: 复用

extension WmcCustomViewProtocol {
    /// 创建模态配置器
    func createPresentConfig(style: WmcPresentVC.Style,
                             view: UIView,
                             layoutBlock: @escaping YPBlock.OneParam<ConstraintMaker>,
                             isTouchMaskDismiss: Bool = true,
                             backgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                             hollowRects: [WmcPresentVC.Config.HollowRect],
                             touchMaskBlock: @escaping YPBlock.Empty = {}) -> WmcPresentVC.Config
    {
        let config: WmcPresentVC.Config = .init(style: style,
                                                contentView: view,
                                                contentLayoutBlock: layoutBlock,
                                                isTouchMaskDismiss: isTouchMaskDismiss,
                                                touchMaskBlock: touchMaskBlock,
                                                backgroundColor: backgroundColor,
                                                hollowRects: hollowRects)
        return config
    }
}

// MARK: 可选方法

public extension WmcCustomViewProtocol {
    /// 设置页面推出时的样式
    func pushTransitionStyle(_ attribute: WmcAttribute) -> WmcTransition.Style {
        return .right
    }

    /// 设置页面退出时的样式
    func popTransitionStyle(_ attribute: WmcAttribute) -> WmcTransition.Style {
        return .right
    }

    /// 设置页面推出时的动画
    func pushTransitionAnimated(_ attribute: WmcAttribute) -> Bool {
        return true
    }

    /// 设置页面退出时的动画
    func popTransitionAnimated(_ attribute: WmcAttribute) -> Bool {
        return true
    }

    /// 视图即将加载
    func viewDidLoad(_ attribute: WmcAttribute) {}

    /// 某个页面准备出现
    func viewWillAppear(_ attribute: WmcAttribute,
                        animated: Bool) {}

    /// 某个页面完全出现
    func viewDidAppear(_ attribute: WmcAttribute,
                       animated: Bool) {}

    /// 某个页面即将消失
    func viewWillDisappear(_ attribute: WmcAttribute,
                           animated: Bool) {}

    /// 某个页面已经消失
    func viewDidDisappear(_ attribute: WmcAttribute,
                          animated: Bool) {}

    /// 点击返回时触发
    func backAction(_ attribute: WmcAttribute) -> Bool {
        return true
    }

    /// 是否使用导航
    func navigationBarUse(_ attribute: WmcAttribute) -> Bool {
        return true
    }

    /// 设置导航条样式
    func navigationBarStyle(_ attribute: WmcAttribute) -> WmcBaseVC.NavStyle {
        return .white
    }

    /// 是否需要导航底部阴影
    func navigationBarUseShadow(_ attribute: WmcAttribute) -> Bool {
        return true
    }

    /// 在这里进行布局UI
    func makeUI(_ attribute: WmcAttribute) {}

    /// 在这里进行绑定视图事件
    func bindViewAction(_ attribute: WmcAttribute) {}

    /// 在这里进行绑定VM事件
    func bindViewModel(_ attribute: WmcAttribute) {}
}
